package com.xbongbong.parent.parse.data;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.AnalysisDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.help.FormDataAnalysisHelp;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 编辑页中关联产品列表的字段解析，主要特殊处理下单位这个字段
 * @author 吴峰
 * @date 2022/10/27 15:31
 */
@Component
public class LinkProductUpdateGetServiceImpl extends FormDataListAnalysisDataHelp {


    /**
     * 解析特殊数据，地址，附件，定位，多选转成逗号隔开的字符串
     * @param resultJsonObject
     */
    @Override
    public void analysisData(AnalysisDataDTO analysisDataDTO, JSONObject resultJsonObject,
                             Map<String, Object> linkData, Set<Object> linkDataSet) throws XbbException {



        FieldAttrEntity fieldAttrEntity = analysisDataDTO.getFieldAttrEntity();
        String key = analysisDataDTO.getAttr();
        JSONObject objectJsonObject = analysisDataDTO.getDataJsonObject();
        // 判断高级规则
        boolean invisibleOfScope = false;
        // 审批摘要不判断高级权限
        if (Objects.isNull(analysisDataDTO.getIsProcess()) || !analysisDataDTO.getIsProcess()) {
            invisibleOfScope = FormDataAnalysisHelp.invisibleSetNull(fieldAttrEntity, analysisDataDTO, objectJsonObject);
        }
        if (Objects.equals(fieldAttrEntity.getSaasAttr(), SelectProductEnum.UNIT.getSaasAttr())) {
            // 关联产品单位特殊处理下
            if (invisibleOfScope) {
                resultJsonObject.put(key, StringConstant.INVISIBLE_VALUE);
            } else {
                resultJsonObject.put(key, objectJsonObject.get(key));
            }
        } else {
            super.analysisData(analysisDataDTO, resultJsonObject, linkData, linkDataSet);
        }
    }

}
